package com.leetcode.easy;

/**
 * @Classname SolutionSearchInsert
 * @Description TODO
 * @Date 2020/8/20 9:24
 * @Created by zfl
 */
public class SolutionSearchInsert {
    /*
    *@Description 搜索插入位置 二分查找
    *@param [nums, target]
    *@return int
    * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
    * 内存消耗：39.3 MB, 在所有 Java 提交中击败了81.29%的用户
    */
    public int searchInsert(int[] nums, int target) {
        int lo = 0;
        int hi = nums.length - 1;
        int mid = 0;
        if(nums.length == 0 || nums == null) {
            return 0;
        }
        while (lo < hi) {
            mid = lo + (hi - lo) / 2;
            if(target < nums[mid]) {
                hi = mid - 1;
            }else if(target > nums[mid]) {
                lo = mid + 1;
            }else {
                return mid;
            }
        }
        if(nums[lo] < target) {
            return lo + 1;
        }else {
            return lo;
        }
    }

}
